<template>
  <div class="roleList">
    <!-- 操作表单 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
      <el-form-item>
        <el-button type="primary" @click="add">增加</el-button>
      </el-form-item>
      <el-form-item label="角色名">
        <el-input v-model="formInline.roleName" placeholder="请输入角色名查询"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="query">查询(还没有实现)</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="roleId" label="角色编号" align="center"></el-table-column>
      <el-table-column prop="roleName" label="角色名" align="center"></el-table-column>
      <el-table-column prop="roleComment" label="备注" align="center"></el-table-column>
      <el-table-column prop="gmtCreate" label="创建时间" align="center" :formatter="formatTime"></el-table-column>
      <el-table-column prop="gmtModified" label="最后修改时间" align="center" :formatter="formatTime"></el-table-column>

      <!-- 操作 -->
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" size="small"
                     @click="updateInfo(scope.row)">
            <el-icon>
              <edit/>
            </el-icon>
          </el-button>
          <el-button type="danger" size="small"
                     @click="deleteInfo(scope.row)">
            <el-icon>
              <delete/>
            </el-icon>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 page.pageNum-->
    <el-row style="margin-top: 10px">
      <el-col :offset="15" :span="6">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="page.pageNum"
          @current-change="changePage"
          :page-count="page.pages"/>
      </el-col>
    </el-row>

    <!-- 新增信息的表单 -->
    <el-dialog :title="state ? '新增角色' : '修改角色'" v-model="dialogFromVisible" width="500px">
      <el-form :model="form">
        <el-form-item label="角色名" :label-width="formLabelWidth">
          <el-input v-model="form.roleName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="form.roleComment" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <span>还没加数据校验</span>
          <el-button type="primary" @click="dialogFromVisible = false">取消</el-button>
          <el-button type="primary" @click="submit('form')">确定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script>
  // import { Edit, Delete} from "@element-plus/icons-vue"

  export default {
    name: "RoleList",
    components: {
      // Edit,
      // Delete,
    },
    data() {
      return {
        formInline: {
          roleName: '',
        },
        tableData: [],
        dialogFromVisible: false,
        state: true,
        noneForm: {
          roleId: '',
          roleName: '',
          roleComment: '',
        },
        form: {
          roleId: '',
          roleName: '',
          roleComment: '',
        },
        formLabelWidth: "80px",

        page: {
          size: 8,
          pageNum: 1,
          pages: 10,
        },
      };
    },
    created() {
      this.changePage(1);
    },
    methods: {
      query() {
        console.log(this.formInline);
      },

      add() {
        console.log("没添加");
        // if (!this.state) this.form = this.noneForm;
        // this.state = true;
        // this.dialogFromVisible = true;
      },
      updateInfo(row) {
        console.log(row, "updateInfo");
        // this.state = false;
        // this.dialogFromVisible = true;
        //
        // this.form.roleId = row.roleId;
        // this.form.roleName = row.roleName;
        // this.form.pwd = "";
        // this.form.type.typeId = row.type.typeId;
        // this.form.nickname = row.nickname;
        // this.form.roleComment = row.roleComment;
      },
      deleteInfo(row) {
        console.log(row, "deleteInfo");
        // this.service.delete("/role/" + row.roleId)
        //   .then(res => {
        //     console.log(res);
        //     this.changePage(this.page.pageNum);
        //   })
        //   .catch(err => {
        //     console.log(err);
        //   });
      },

      submit() {
        if (this.form.pwd === "") this.form.pwd = null
        if (this.state) {
          this.service.post('/role', this.form)
            .then(res => {
              console.log(res);
              let data = res.data.data;
              this.tableData.splice(0, 0, data.role);
            })
            .catch(err => {
              console.log(err);
            })
        } else {
          console.log(this.form, "发送请求前的 this.form ")
          this.service.put('/role', this.form)
            .then(res => {
              console.log(res);
              let i;
              for (i = 0; i < this.tableData.length; i++) {
                if (this.tableData[i].roleId === this.form.roleId)
                  break;
              }
              this.tableData.splice(i, 1, res.data.data.role)
            })
            .catch(err => {
              console.log(err);
            })
        }
        this.dialogFromVisible = false;
      },
      formatTime(row, column, cellValue) {
        let time1 = cellValue.substring(0, 10);
        let time2 = cellValue.substring(11, 19);
        return time1 + ' ' + time2;
      },
      changePage(pageNum) {
        console.log("页码改变到第 " + pageNum + " 页");
        this.service.get('role/' + pageNum + '/' + this.page.size)
          .then(res => {
            console.log(res);
            let data = res.data.data;
            this.tableData = data.page.list;
            this.page.pageNum = data.page.pageNum;
            this.page.pages = data.page.pages;
          })
          .catch(err => {
            console.error(err);
          });
      },
    },
  }

</script>

<style scoped lang="scss">
  .roleList {
    .demo-form-inline, .el-form-item {
      text-align: left;
    }

    .el-select {
      width: 100%;
    }

    .dialog-footer {
      text-align: center;
    }
  }
</style>